package com.betterjr.modules.account.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.Collections3;
import com.betterjr.modules.account.dao.CustOperatorRelationMapper;
import com.betterjr.modules.account.entity.CustOperatorRelation;

@Service
public class CustAndOperatorRelaService extends BaseService<CustOperatorRelationMapper, CustOperatorRelation> {

    /**
     * 
     * 检查操作员和客户关系
     * @param 操作员编号
     * @param1 客户号
     * @return 如果存在操作关系，则返回True,否则返回False
     * @throws 异常情况
     */
    public boolean checkOperatorAndCustNo(final Long anOperNo, final Long anCustNo) {
        if (anOperNo != null && anCustNo != null) {
            final Map<String, Object> map = new HashMap();
            map.put("operNo", anOperNo);
            map.put("custNo", anCustNo);
            final List list = this.selectByProperty(map);

            return list.size() > 0;
        }

        return false;
    }

    public List<Long> findCustNoList(final String anOperOrg) {

        return this.findCustNoList(null, anOperOrg);
    }

    public List<Long> findCustNoList(final Long anOperNo, final String anOperOrg) {
        Map<String, Object> map = new HashMap();
        map.put("operNo", anOperNo);
        map.put("operOrg", anOperOrg);
        map.put("status", "1");

        // 过滤空条件 兼容 方法findCustNoList
        map = Collections3.filterMapEmptyObject(map);

        final List<Long> custNoList = new ArrayList<>();
        final Set<Long> unRepeatCustNoSet = new HashSet<>();
        for (final CustOperatorRelation custOR : this.selectByProperty(map)) {
            unRepeatCustNoSet.add(custOR.getCustNo());
        }
        // 去除重复的企业编号
        custNoList.addAll(unRepeatCustNoSet);
        return custNoList;
    }

    public List<Long> findOperNoList(final Long anCustNo) {
        final Map<String, Object> map = new HashMap();
        map.put("custNo", anCustNo);
        final List<Long> custNoList = new ArrayList<Long>();
        for (final CustOperatorRelation custOR : this.selectByProperty(map)) {
            custNoList.add(custOR.getOperNo());
        }
        return custNoList;
    }

    public String findOperOrgByCustNo(final Long anCustNo) {
        final List<CustOperatorRelation> tmpList = this.selectByProperty("custNo", anCustNo);
        if (Collections3.isEmpty(tmpList)) {
            return "";
        } else {
            return Collections3.getFirst(tmpList).getOperOrg();
        }
    }

    /****
     * 查询客户操作员绑定关系信息
     * @param anOperId
     * @param anOperOrg
     * @return
     */
    public String findCustOperator(final Long anOperId, final String anOperOrg) {
        final StringBuffer sb = new StringBuffer();
        final Map<String, Object> map = new HashMap();
        map.put("operOrg", anOperOrg);
        map.put("operNo", anOperId);
        map.put("status", "1");
        int i = 0;
        for (final CustOperatorRelation custOR : this.selectByProperty(map)) {
            if (i == 0) {
                sb.append(custOR.getCustNo());
            } else {
                sb.append("," + custOR.getCustNo());
            }
            i++;
        }
        return sb.toString();
    }
    
    /**
     * 查找客户
     */
    public String findCustOperatorAfter(final Long anOperId) {
        final StringBuffer sb = new StringBuffer();
        final Map<String, Object> map = new HashMap();
        map.put("operNo", anOperId);
        map.put("status", "1");
        int i = 0;
        for (final CustOperatorRelation custOR : this.selectByProperty(map)) {
            if (i == 0) {
                sb.append(custOR.getCustNo());
            } else {
                sb.append("," + custOR.getCustNo());
            }
            i++;
        }
        return sb.toString();
    }

    /***
     * 添加绑定客户操作员
     * @param anCustList
     */
    public void addCustOperatorRelation(final Long anOperId, final String anOperOrg, final String anCustList) {
        final Map<String, Object> operatorMp = new HashMap<String, Object>();
        operatorMp.put("operNo", anOperId);
        operatorMp.put("operOrg", anOperOrg);
        this.deleteByExample(operatorMp);

        final String[] custArr = anCustList.split(",");
        for (int i = 0; i < custArr.length; i++) {
            final String custStr = custArr[i];
            // 增加操作员和客户之间关系
            this.insert(new CustOperatorRelation(anOperId, Long.parseLong(custStr), anOperOrg));
        }
    }

    /**
     * 
     * @Title: addCustOperatorRelationAfter 
     * @Description: TODO(增加操作员与企业的关系) 
     * @param @param anOperId
     * @param @param anOperOrg
     * @param @param anCustList 参数说明 
     * @return void 返回类型 
     * @throws 
     * @author chenlq
     * @date 2018年6月30日 上午10:04:31
     */
    public void addCustOperatorRelationAfter(final Long anOperId, final String anOperOrg) {
        final Map<String, Object> operatorMp = new HashMap<String, Object>();
        operatorMp.put("operNo", anOperId);
        operatorMp.put("operOrg", anOperOrg);
        this.deleteByExample(operatorMp);
        final CustOperatorRelation custOperatorRelation = new CustOperatorRelation();
        custOperatorRelation.initOperatorInfo(anOperId, anOperOrg);
        // 增加操作员和客户之间关系
        this.insert(custOperatorRelation);

    }

    public CustOperatorRelation saveCustRelation(final Long anOperId, final Long anCustNo, final String anOperOrg) {
        final Map<String, Object> termMap = new HashMap();
        termMap.put("operNo", anOperId);
        termMap.put("custNo", anCustNo);

        CustOperatorRelation opRelation = Collections3.getFirst(this.selectByProperty(termMap));
        if (opRelation == null) {
            opRelation = new CustOperatorRelation(anOperId, anCustNo, anOperOrg);
            this.insert(opRelation);
        }

        return opRelation;
    }
}
